﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TELOS_Konverter
{
    class HtmlHandler
    {
        private string fileName;
        private const int PLAYERSIZE_SMALL = 1;
        private const int PLAYERSIZE_BIG = 2;

        private MessageHandler mh = new MessageHandler();
        private FileOperations fo = new FileOperations();
        private ErrorHandler eh = new ErrorHandler();
        private StringHandler sth = new StringHandler();

        public bool rewriteHTMLContent(String fName)
        {
            this.fileName = fName;
            List<string> htmlContent = fo.readFile(fName);
            htmlContent = setMediaType(htmlContent);
            htmlContent = insertNewPlayerCode(htmlContent);
            return fo.writeFile(fName, htmlContent);
        }

        private int getPlayerSize() {
            if (sth.isStringInString(this.fileName, "_t.html")) {
                return PLAYERSIZE_SMALL;
            } else 
                return PLAYERSIZE_BIG;
        }

        /**setMediaType
         * This function changes the mediatype from "1" to "2" and will change the websites behavior to switch into another if-case
         * @param: String array containing the website, split in lines
         * @return: String array containing the website and with the media type replaced with 2 instead of 1
         */
        private List<string> setMediaType(List<string> website)
        {
            for (int i = 0; i < website.Count; i++)
            {
                if (website[i].IndexOf("mediatype") >= 0)
                {
                    website[i] = website[i].Replace("1", "2");
                    break;
                }
            }
            return website;
        }

        /**insertNewMediaPlayer
         * @param: String array containing the website, split in lines
         * @return: String array containing the website and with the new player code inserted
         * This function will write all the code to implement the player in the HTML website
         */
        private List<string> insertNewPlayerCode(List<string> website)
        {
            // 1. Find line that has " (real==false||mediatype=="2") " in it
            int mediatypeLine = 0;
            for (int i = 0; i < website.Count; i++)
            {
                if (website[i].IndexOf("(real==false||mediatype==\"2\")") >= 0)
                {
                    mediatypeLine = i;
                    break;
                }
            }

            // 2. Delete all lines after than one until there comes a "}"
            List<string> list = new List<string>(website);
            for (int i = mediatypeLine + 1; i < list.Count; i++)
            {
                if (list[i].IndexOf("}") >= 0)
                {
                    break;
                }
                else
                {
                    try
                    {
                        list.RemoveAt(i);
                        i--;
                    }
                    catch (Exception e)
                    {
                        eh.createError(e);
                        break;
                    }
                }
            }

            string width = "";
            string height = "";
            string subtitleSpace = "";
            // player size depends upon the file name
            // all files that end with "_t" are for the top frame and will contain a smaller player
            if (getPlayerSize() == PLAYERSIZE_SMALL)
            {
                width = "150";
                height = "40";
            }
            else if (getPlayerSize() == PLAYERSIZE_BIG)
            {
                width = "320";
                height = "45";
                subtitleSpace = "  document.write('<div style = \"display:display_caption; width:320; height:70; background-color:black;\" id = CapText></div>');";
            }

            // 3. Insert the video player lines
            list.Insert(mediatypeLine + 1, "  document.write('<OBJECT ID=\"MediaPlayer\" classid=\"CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95\" CODEBASE=\"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715\" width=\""+width+"\" height=\""+height+"\" standby=\"Loading Microsoft Windows Media Player components...\" type=\"application/x-oleobject\">');");
            list.Insert(mediatypeLine + 2, "  document.write('<PARAM NAME=\"FileName\" VALUE=\"" + fo.getASXFileName() + "\">');");
            list.Insert(mediatypeLine + 3, "  document.write('<PARAM NAME=\"captioningID\" VALUE=\"CapText\">');");
            list.Insert(mediatypeLine + 4, "  document.write('<PARAM NAME=\"Autostart\" VALUE=\"false\">');");
            list.Insert(mediatypeLine + 5, "  document.write('<embed captioningID=\"CapText\" autostart=\"0\" type=\"application/x-mplayer2\" pluginspage=\"http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/\" src=\"" + fo.getASXFileName() + "\" Name=\"MediaPlayer\" width=\""+width+"\" height=\""+height+"\"></embed></OBJECT>');");
            list.Insert(mediatypeLine + 6, subtitleSpace);

            return list;
        }
    }
}
